﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.Revit.RevitExtension;
using YJKArchUtils.Utils;

namespace YJKCarSlopingPath
{
	public class Previewer
	{
		public List<CurveInfo> Curves
		{
			set
			{
				if (value != null)
				{
					this.m_Curves = value;
				}
			}
		}

		public List<TextNoteInfo> TextNotes
		{
			set
			{
				if (value != null)
				{
					this.m_TextNotes = value;
				}
			}
		}

		public Previewer(System.Windows.Forms.Form parent, int left, int top, int width, int height, uint messageFlag)
		{
			this.m_PictureBoxView = new PictureBox();
			this.m_PictureBoxView.BackColor = System.Drawing.Color.White;
			this.m_PictureBoxView.BorderStyle = BorderStyle.Fixed3D;
			this.m_PictureBoxView.Location = new System.Drawing.Point(left, top);
			this.m_PictureBoxView.Size = new Size(width, height);
			this.m_PictureBoxView.SizeMode = PictureBoxSizeMode.AutoSize;
			this.m_PictureBoxView.TabStop = false;
			this.m_PictureBoxView.Parent = parent;
			this.m_PictureBoxView.Paint += this.PictureBox_Paint;
			this.m_PictureBoxView.MouseDoubleClick += this.PictureBox_MouseDoubleClick;
			this.m_PictureBoxView.MouseDown += this.PictureBox_MouseDown;
			this.m_PictureBoxView.MouseEnter += this.PictureBox_MouseEnter;
			this.m_PictureBoxView.MouseLeave += this.PictureBox_MouseLeave;
			this.m_PictureBoxView.MouseMove += this.PictureBox_MouseMove;
			this.m_PictureBoxView.MouseUp += this.PictureBox_MouseUp;
			this.m_MessageFlag = messageFlag;
			this.InitData();
		}

		public void MouseWheel(object sender, MouseEventArgs e)
		{
			if (this.m_PictureBoxView.Focused)
			{
				float num = 5f;
				float zoom = this.GetZoom();
				if (Geometry.Lessthan_Or_Equal((double)zoom, (double)(1f / num)) && e.Delta < 0)
				{
					return;
				}
				if (Geometry.Greaterthan_Or_Equal((double)zoom, (double)num) && e.Delta > 0)
				{
					return;
				}
				this.m_ZoomDelta += (float)e.Delta;
				float num2 = (this.GetZoom() - zoom) / zoom;
				System.Drawing.Point point = new System.Drawing.Point(e.X - this.m_PictureBoxView.Left, e.Y - this.m_PictureBoxView.Top);
				float num3 = this.m_OriginX - (float)point.X;
				float num4 = this.m_OriginY - (float)point.Y;
				this.m_OriginX += num3 * num2;
				this.m_OriginY += num4 * num2;
				this.m_PictureBoxView.Parent.Refresh();
			}
		}

		private void InitData()
		{
			this.m_ZoomDelta = 0f;
			this.m_MouseDown = false;
			this.m_PtDown = System.Drawing.Point.Empty;
			this.m_PtUp = System.Drawing.Point.Empty;
			Size size = this.m_PictureBoxView.Size;
			this.m_OriginX = (float)(size.Width / 2);
			this.m_OriginY = (float)(size.Height / 2);
		}

		private float GetZoom()
		{
			Size size = this.m_PictureBoxView.Size;
			float num = this.m_ZoomDelta / (float)size.Width / 5f;
			return 1f + num;
		}

		[DllImport("User32.dll")]
		private static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void PictureBox_Paint(object sender, PaintEventArgs e)
		{
			Size size = this.m_PictureBoxView.Size;
			float zoom = this.GetZoom();
			e.Graphics.Transform = new Matrix(zoom, 0f, 0f, zoom, this.m_OriginX, this.m_OriginY);
			Previewer.SendMessage(this.m_PictureBoxView.Parent.Handle, 1125, new IntPtr((long)((ulong)this.m_MessageFlag)), IntPtr.Zero);
			for (int i = 0; i < this.m_Curves.Count; i++)
			{
				CurveInfo curveInfo = this.m_Curves[i];
				this.m_Curves[i] = new CurveInfo(curveInfo.CurveObj, curveInfo.Color, curveInfo.LineWidth / zoom);
			}
			new DrawingPlotter(this.m_PictureBoxView, e.Graphics, this.m_Curves, this.m_TextNotes).Draw();
		}

		private void PictureBox_MouseEnter(object sender, EventArgs e)
		{
			this.m_PictureBoxView.Focus();
		}

		private void PictureBox_MouseLeave(object sender, EventArgs e)
		{
			((System.Windows.Forms.Form)this.m_PictureBoxView.Parent).ActiveControl = null;
		}

		private void PictureBox_MouseDown(object sender, MouseEventArgs e)
		{
			this.m_PtDown = new System.Drawing.Point(e.X, e.Y);
			this.m_MouseDown = true;
			this.m_PictureBoxView.Capture = true;
		}

		private void PictureBox_MouseUp(object sender, MouseEventArgs e)
		{
			if (!this.m_PtUp.IsEmpty)
			{
				this.m_OriginX += (float)(this.m_PtUp.X - this.m_PtDown.X);
				this.m_OriginY += (float)(this.m_PtUp.Y - this.m_PtDown.Y);
			}
			this.m_PtUp = System.Drawing.Point.Empty;
			this.m_MouseDown = false;
			this.m_PictureBoxView.Capture = false;
			this.m_PictureBoxView.Parent.Refresh();
		}

		private void PictureBox_MouseMove(object sender, MouseEventArgs e)
		{
			if (this.m_MouseDown)
			{
				System.Drawing.Point end = System.Drawing.Point.Empty;
				if (this.m_PtUp != System.Drawing.Point.Empty)
				{
					end = this.m_PictureBoxView.PointToScreen(this.m_PtUp);
					ControlPaint.DrawReversibleLine(this.m_PictureBoxView.PointToScreen(this.m_PtDown), end, System.Drawing.Color.Black);
				}
				System.Drawing.Point ptUp = this.FormatPoint(new System.Drawing.Point(e.X, e.Y));
				this.m_PtUp = ptUp;
				end = this.m_PictureBoxView.PointToScreen(this.m_PtUp);
				ControlPaint.DrawReversibleLine(this.m_PictureBoxView.PointToScreen(this.m_PtDown), end, System.Drawing.Color.Black);
			}
		}

		private void PictureBox_MouseDoubleClick(object sender, MouseEventArgs e)
		{
			this.InitData();
		}

		private System.Drawing.Point FormatPoint(System.Drawing.Point ptCurrent)
		{
			int num = 2;
			int x = this.m_PictureBoxView.Width - 2 * num;
			int y = this.m_PictureBoxView.Height - 2 * num;
			System.Drawing.Point point = new System.Drawing.Point(-num, -num);
			System.Drawing.Point point2 = new System.Drawing.Point(x, -num);
			System.Drawing.Point point3 = new System.Drawing.Point(x, y);
			System.Drawing.Point point4 = new System.Drawing.Point(-num, y);
			System.Drawing.Point empty = System.Drawing.Point.Empty;
			if (this.Intersect2Line(this.m_PtDown, ptCurrent, point, point2, ref empty))
			{
				return empty;
			}
			if (this.Intersect2Line(this.m_PtDown, ptCurrent, point2, point3, ref empty))
			{
				return empty;
			}
			if (this.Intersect2Line(this.m_PtDown, ptCurrent, point3, point4, ref empty))
			{
				return empty;
			}
			if (this.Intersect2Line(this.m_PtDown, ptCurrent, point4, point, ref empty))
			{
				return empty;
			}
			return ptCurrent;
		}

		private bool Intersect2Line(System.Drawing.Point ptS1, System.Drawing.Point ptE1, System.Drawing.Point ptS2, System.Drawing.Point ptE2, ref System.Drawing.Point ptInter)
		{
			Line line = null;
			Line line2 = null;
			try
			{
				line = YJKLineEx.YJKGetBound(new XYZ((double)ptS1.X, (double)ptS1.Y, 0.0), new XYZ((double)ptE1.X, (double)ptE1.Y, 0.0));
				line2 = YJKLineEx.YJKGetBound(new XYZ((double)ptS2.X, (double)ptS2.Y, 0.0), new XYZ((double)ptE2.X, (double)ptE2.Y, 0.0));
			}
			catch (Exception)
			{
				return false;
			}
			IntersectionResultArray intersectionResultArray;
            if ((int)line.Intersect(line2, out intersectionResultArray) == 8 && intersectionResultArray.Size > 0)
			{
				XYZ xyzpoint = intersectionResultArray.get_Item(0).XYZPoint;
				ptInter = new System.Drawing.Point((int)xyzpoint.X, (int)xyzpoint.Y);
				return true;
			}
			return false;
		}

		public const int USER = 1024;

		public const int WM_FICBOX_PAINT = 1125;

		private uint m_MessageFlag;

		private PictureBox m_PictureBoxView;

		private List<CurveInfo> m_Curves = new List<CurveInfo>();

		private List<TextNoteInfo> m_TextNotes = new List<TextNoteInfo>();

		private float m_ZoomDelta;

		private bool m_MouseDown;

		private System.Drawing.Point m_PtDown = System.Drawing.Point.Empty;

		private System.Drawing.Point m_PtUp = System.Drawing.Point.Empty;

		private float m_OriginX;

		private float m_OriginY;
	}
}
